<template>
  <div class="login-page">
    <Header />
    
    <div class="login-container">
      <!-- 左侧宣传区域 -->
      <div class="login-left">
        <div class="content-wrapper">
          <h1>欢迎使用智能管理系统</h1>
          <p class="subtitle">高效 · 安全 · 智能</p>
          <p class="description">
            我们的系统提供全方位的管理解决方案，<br>
            助您轻松应对各种业务挑战。
          </p>
          <div class="features">
            <div class="feature-item">
              <i class="icon">✓</i>
              <span>数据安全保障</span>
            </div>
            <div class="feature-item">
              <i class="icon">✓</i>
              <span>多终端支持</span>
            </div>
            <div class="feature-item">
              <i class="icon">✓</i>
              <span>实时数据分析</span>
            </div>
          </div>
        </div>
        <div class="background-overlay"></div>
        <div class="background-image"></div>
      </div>
      
      <!-- 右侧登录区域 -->
      <div class="login-right">
        <div class="login-card">
          <div class="login-header">
            <h2>用户登录</h2>
            <p>请输入您的账号信息</p>
          </div>
          
          <div class="login-body">
            <form @submit.prevent="handleSubmit">
              <div class="form-group">
                <label for="username">用户名</label>
                <div class="input-wrapper">
                  <i class="icon">👤</i>
                  <input 
                    type="text" 
                    id="username" 
                    v-model="form.username" 
                    class="form-control" 
                    :class="{ 'is-invalid': errors.username }"
                    placeholder="请输入用户名"
                    required
                  >
                </div>
                <div class="invalid-feedback" v-if="errors.username">
                  {{ errors.username }}
                </div>
              </div>
              
              <div class="form-group">
                <label for="password">密码</label>
                <div class="input-wrapper">
                  <i class="icon">🔒</i>
                  <input 
                    type="password" 
                    id="password" 
                    v-model="form.password" 
                    class="form-control" 
                    :class="{ 'is-invalid': errors.password }"
                    placeholder="请输入密码"
                    required
                  >
                </div>
                <div class="invalid-feedback" v-if="errors.password">
                  {{ errors.password }}
                </div>
              </div>
               <!-- 验证码区域 -->
              <div class="form-group">
                <label for="code">验证码</label>
                <div class="captcha-container">
                  <div class="input-wrapper">
                    <i class="icon">🔐</i>
                    <input 
                      type="text" 
                      id="code" 
                      v-model="form.code" 
                      class="form-control" 
                      :class="{ 'is-invalid': errors.code }"
                      placeholder="请输入验证码"
                      required
                      maxlength="4"
                    >
                  </div>
                  <div class="captcha-image" @click="refreshCaptcha">
                    <img v-if="captcha.img" :src="captcha.img" alt="验证码">
                    <span v-else>加载验证码...</span>
                  </div>
                </div>
                <div class="invalid-feedback" v-if="errors.code">
                  {{ errors.code }}
                </div>
              </div>
              <div class="form-options">
                <div class="form-check">
                  <input type="checkbox" id="remember" v-model="form.remember" class="form-check-input">
                  <label for="remember" class="form-check-label">记住我</label>
                </div>
                <router-link to="/forgot-password" class="forgot-password">忘记密码?</router-link>
              </div>
              
              <div class="form-group">
                <button type="submit" class="btn btn-primary" :disabled="loading">
                  <span v-if="loading" class="spinner-border spinner-border-sm" role="status"></span>
                  登录
                </button>
              </div>
              
              <div class="register-link">
                还没有账号? <router-link to="/register">立即注册</router-link>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
    
    <Footer />
  </div>
</template>

<script>
import Header from '@/components/Header'
import Footer from '@/components/Footer'
import { login, getCaptchaImage } from '@/api/user'
import { setToken } from '@/utils/auth'

export default {
  name: 'Login',
  components: {
    Header,
    Footer
  },
  
  data() {
    return {
      form: {
        username: '',
        password: '',
        code: '',
        remember: false,
        uuid: ''
      },
       captcha: {
        img: '',
        uuid: ''
      },
      errors: {},
      loading: false
    }
  },
  created() {
    this.refreshCaptcha()
  },
  methods: {
    // 刷新验证码
    async refreshCaptcha() {
      try {
        const res = await getCaptchaImage()
        console.log(res)
        if (res.code === 200) {
          this.captcha.img = 'data:image/png;base64,' + res.img
          this.captcha.uuid = res.uuid
          this.form.uuid = res.uuid
        } else {
          this.$notify.error({
            title: '错误',
            message: '验证码获取失败'
          })
        }
      } catch (error) {
         console.error('验证码加载错误:', error)  // 调试用
        this.$notify.error({
          title: '错误',
          message: '验证码加载失败，请刷新页面重试'
        })
      }
    },
    async handleSubmit() {
      this.errors = {}
      this.loading = true
      // 验证表单数据
      if (!this.form.username) {
        this.errors.username = '请输入用户名'
        this.loading = false
        return
      }
      
      if (!this.form.password) {
        this.errors.password = '请输入密码'
        this.loading = false
        return
      }
      
      if (!this.form.code) {
        this.errors.code = '请输入验证码'
        this.loading = false
        return
      }
      try {
         const submitData = {
          ...this.form,
          uuid: this.captcha.uuid
        }
        const res = await login(submitData)
        // setToken(res.token, this.form.remember)
         await this.$store.dispatch('user/login', {
        token: res.token,
        remember: this.form.remember
      })
        // 跳转到首页或之前的路由
        const redirect = this.$route.query.redirect || '/'
        this.$router.push(redirect)
        
        this.$notify({
          title: '成功',
          message: '登录成功',
          type: 'success'
        })
      } catch (error) {
        if (error.response && error.response.status === 400) {
          this.errors = error.response.data.errors || {}
          this.$notify.error({
            title: '错误',
            message: error.response.data.message || '登录失败'
          })
        } else {
          this.$notify.error({
            title: '错误',
            message: '网络错误，请稍后重试'
          })
        }
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f8f9fa;
}

.login-container {
  display: flex;
  flex: 1;
  min-height: calc(100vh - 120px);
}

/* 左侧区域样式 */
.login-left {
  flex: 1;
  position: relative;
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  overflow: hidden;
}

.content-wrapper {
  position: relative;
  z-index: 3;
  max-width: 600px;
  padding: 40px;
  text-align: center;
}

.login-left h1 {
  font-size: 2.5rem;
  margin-bottom: 1rem;
  font-weight: 600;
  line-height: 1.2;
}

.login-left .subtitle {
  font-size: 1.5rem;
  margin-bottom: 1.5rem;
  color: rgba(255, 255, 255, 0.9);
}

.login-left .description {
  font-size: 1.1rem;
  line-height: 1.6;
  margin-bottom: 2rem;
  opacity: 0.9;
}

.features {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 20px;
  margin-top: 2rem;
}

.feature-item {
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.1);
  padding: 10px 20px;
  border-radius: 50px;
  backdrop-filter: blur(5px);
}

.feature-item .icon {
  margin-right: 8px;
  font-weight: bold;
}

.background-image {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: url('https://images.unsplash.com/photo-1551288049-bebda4e38f71?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80');
  background-size: cover;
  background-position: center;
  z-index: 1;
}

.background-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #3498db 0%, #2c3e50 100%);
  opacity: 0.85;
  z-index: 2;
}

/* 右侧登录区域样式 */
.login-right {
  width: 500px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px 40px;
  background-color: white;
  box-shadow: -5px 0 15px rgba(0, 0, 0, 0.05);
}

.login-card {
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-size: 1.8rem;
  color: #2c3e50;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.login-header p {
  color: #7f8c8d;
  font-size: 0.95rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: #2c3e50;
  font-size: 0.95rem;
}

.input-wrapper {
  position: relative;
}

.input-wrapper .icon {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 1.1rem;
}

.form-control {
  width: 100%;
  padding: 12px 15px 12px 45px;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background-color: #f9f9f9;
}

.form-control:focus {
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
  outline: none;
  background-color: white;
}

.is-invalid {
  border-color: #e74c3c;
}

.invalid-feedback {
  color: #e74c3c;
  font-size: 0.85rem;
  margin-top: 0.25rem;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.form-check {
  display: flex;
  align-items: center;
}

.form-check-input {
  margin-right: 0.5rem;
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.forgot-password {
  color: #3498db;
  font-size: 0.9rem;
  text-decoration: none;
  transition: color 0.2s;
}

.forgot-password:hover {
  color: #2980b9;
  text-decoration: underline;
}

.btn-primary {
  width: 100%;
  padding: 12px;
  background-color: #3498db;
  border: none;
  border-radius: 6px;
  color: white;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 5px rgba(52, 152, 219, 0.2);
}

.btn-primary:hover {
  background-color: #2980b9;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(52, 152, 219, 0.3);
}

.btn-primary:active {
  transform: translateY(0);
}

.btn-primary:disabled {
  background-color: #bdc3c7;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.register-link {
  text-align: center;
  margin-top: 1.5rem;
  color: #7f8c8d;
  font-size: 0.95rem;
}

.register-link a {
  color: #3498db;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.2s;
}

.register-link a:hover {
  color: #2980b9;
  text-decoration: underline;
}

/* 底部快捷菜单样式 */
.footer {
  background-color: #2c3e50;
  color: white;
  padding: 20px 0;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.footer .quick-links {
  display: flex;
  justify-content: center;
  padding: 0;
  margin: 0;
  list-style: none;
}

.footer .quick-links li {
  margin: 0 15px;
  position: relative;
}

.footer .quick-links li:not(:last-child)::after {
  content: "•";
  position: absolute;
  right: -18px;
  color: rgba(255, 255, 255, 0.3);
}

.footer .quick-links a {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  font-size: 0.9rem;
  transition: all 0.2s ease;
  display: inline-block;
  padding: 5px 0;
}

.footer .quick-links a:hover {
  color: white;
  transform: translateY(-2px);
}

/* 响应式设计 */
@media (max-width: 992px) {
  .login-container {
    flex-direction: column;
  }
  
  .login-left {
    padding: 60px 20px;
    min-height: auto;
  }
  
  .login-right {
    width: 100%;
    padding: 40px 20px;
    box-shadow: none;
  }
  
  .footer .quick-links {
    flex-wrap: wrap;
  }
  
  .footer .quick-links li {
    margin: 5px 15px;
  }
  
  .footer .quick-links li:not(:last-child)::after {
    display: none;
  }
}

@media (max-width: 576px) {
  .login-left h1 {
    font-size: 2rem;
  }
  
  .login-left .subtitle {
    font-size: 1.2rem;
  }
  
  .features {
    flex-direction: column;
    gap: 10px;
  }
}

/* 验证码容器样式 */
.captcha-container {
  display: flex;
  gap: 10px;
  align-items: center; /* 垂直居中 */
  position: relative; /* 确保子元素定位正确 */
  z-index: 0; /* 默认层级 */
}

.captcha-image {
 width: 150px;  /* 增加宽度 */
  height: 50px;  /* 增加高度 */
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
  background-color: #f5f5f5;
  transition: all 0.3s ease;
   z-index: 1; /* 确保在输入框之上 */
    flex-shrink: 0; /* 防止被挤压 */
}

.captcha-image:hover {
  border-color: #3498db;
}

.captcha-image img {
 width: 100%;
  height: 100%;
  object-fit: contain; /* 保持比例完整显示 */
}

.captcha-image span {
  color: #7f8c8d;
  font-size: 0.8rem;
}

/* 响应式调整 */
@media (max-width: 576px) {
  .captcha-container {
    flex-direction: column;
  }
  
  .captcha-image {
    width: 100%;
    height: 45px;
  }
}
</style>